<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class CI_Bet_manual_tournament_entity{
	var $id = 0;
	var $category_id = 0;
	var $user_id = 0;
	var $stake = 0;
	var $status = "";
	var $result = "";
	var $bet_time = 0;
	var $tournament_id = 0;
	var $point_get = 0;
	var $odds = 0;
	var $answer = "";
	
	function __destruct() {}
	function __construct() {}
    
	function init($cmd, $input)
	{
		$ci=& get_instance();
		
		if($cmd == 'id')
		{
			$result = $ci->db->where('id',$input)->get(TBL_BET_MANUAL_TOURNAMENT)->result();
		}
		else
		{
			return false;
		}
		
		if( !empty($result) )
		{
			$this->id = $result[0]->id;
			$this->category_id = $result[0]->category_id;
			$this->user_id = $result[0]->user_id;
			$this->stake = $result[0]->stake;
			$this->status = $result[0]->status;
			$this->result = $result[0]->result;
			$this->bet_time = $result[0]->bet_time;
			$this->tournament_id = $result[0]->tournament_id;
			$this->point_get = $result[0]->point_get;
			$this->odds = $result[0]->odds;
			$this->odds = $result[0]->odds;
			$this->answer = $result[0]->answer;
			
			return $this;
		}
		return false;
	}
	
	function setCategoryId($input){ $this->category_id=$input; }
	function setUserId($input){ $this->user_id=$input;}
	function setStake($input){ $this->stake=$input; }
	function setStatus($input){ $this->status=$input; }
	function setResult($input){ $this->result=$input; }
	function setBetTime($input){ $this->bet_time=$input; }
	function setTournamentId($input){ $this->tournament_id=$input; }
	function setPointGet($input){ $this->point_get=$input; }
	function setOdds($input){ $this->odds= $input; }
	function setAnswer($input){ $this->answer= $input; }
	
	function getId(){ return $this->id; }
	function getCategoryId(){ return $this->category_id; }
	function getUserId(){ return $this->user_id; }
	function getStake(){ return $this->stake; }
	function getStatus(){ return $this->status; }
	function getResult(){ return $this->result; }
	function getBetTime(){ return $this->bet_time; }
	function getTournamentId(){ return $this->tournament_id; }
	function getPointGet(){ return $this->point_get; }
	function getOdds(){ return $this->odds; }
	function getAnswer(){ return $this->answer; }
	
	function update()
	{
		$ci=& get_instance();
		$ci->db->set('category_id', $this->category_id); 
		$ci->db->set('user_id', $this->user_id);
		$ci->db->set('stake', $this->stake);
		$ci->db->set('status', $this->status);
		$ci->db->set('result', $this->result);
		$ci->db->set('bet_time', $this->bet_time);
		$ci->db->set('tournament_id', $this->tournament_id);
		$ci->db->set('point_get', $this->point_get);
		$ci->db->set('odds', $this->odds );
		$ci->db->set('answer', $this->answer );
		
		$ci->db->where('id', $this->id)->update(TBL_BET_MANUAL_TOURNAMENT);
	}
	
	function createNewBetEntity(){
		$ci=& get_instance();
		$ci->db->set('result', '');
		$ci->db->insert(TBL_BET_MANUAL_TOURNAMENT);
		
		return $this->init('id',$ci->db->insert_id());
	}
	
	function getTotalStakeUserBettedCurrently($categoryId, $userId){
		$ci=& get_instance();
		$sum = $ci->db->query("SELECT SUM(stake) AS total FROM ".TBL_BET_MANUAL_TOURNAMENT." WHERE category_id='$categoryId' AND user_id=$userId")->result();
		return $sum[0]->total;
	}
	
	function processUserBetResult(){
		$ci=& get_instance();
		$USER = $ci->user_entity->init('id',$this->user_id);
		$user_tournament = $ci->user_tournament_entity->findUserInTournament($this->user_id, $this->tournament_id);
		$USER_TOURNAMENT = $ci->user_tournament_entity->init('id',$user_tournament->id);
		
		switch($this->result){
			case 'won':
				$pointGet = round( (float)( $this->getOdds() * $this->getStake() ), 2 );
				$USER_TOURNAMENT->setPoint($USER_TOURNAMENT->getPoint() + $pointGet);
				$USER_TOURNAMENT->update();
				
				$this->setPointGet($pointGet);
				$this->update();
				break;
			case 'lost':
				break;
			case 'cancelled':
				$USER->setCoin($USER->getCoin() + $this->getStake());
				$USER->update();
				break;
			case 'pending':
				break;
		}
	}
	
	function getBetList($cat_key_id){
		$ci=& get_instance();
		return $ci->db->where('category_id',$cat_key_id)->get(TBL_BET_MANUAL_TOURNAMENT)->result();
	}
	
	function isBettingStatusCategory($cat_key_id){
		$ci=& get_instance();
		$betting = false;
		$betResult = $ci->db->where('category_id',$cat_key_id)->get(TBL_BET_MANUAL_TOURNAMENT)->result();
		foreach($betResult as $item){
			if($item->result == 'pending' AND $item->status == 'betting'){
				$betting = true;
				return $betting;
			}
		}
		return $betting;
	}
	
	function getBettingList($cat_key_id){
		$ci=& get_instance();
		return $ci->db->where(array('category_id'=>$cat_key_id, 'status'=>'betting'))->get(TBL_BET_MANUAL_TOURNAMENT)->result();
	}
	
	function processApplyAnswer($cat_key_id){
		$ci=& get_instance();
		$CAT = $ci->category_manual_tournament_entity->init('key_id', $cat_key_id);
		if(!$CAT->getCorrectAnswer()){return;} 
		
		$type = $CAT->getType();
		
		$listBetting = $ci->db->where(array('status'=>'betting','category_id'=>$cat_key_id))->get(TBL_BET_MANUAL_TOURNAMENT)->result();
		foreach($listBetting as $item){
			$answer_array = array_filter(explode('{[|SEPSTR|]}',strtoupper( $item->answer) ));
			array_walk($answer_array, 'trim_value');
		 	
			if($type == 'box'){
				$correctAnswer = strtoupper( $CAT->getCorrectAnswer() );
				if(in_array($correctAnswer,$answer_array)){
					$ci->db->set('result', 'won');
				}else{
					$ci->db->set('result', 'lost');
				}
				$ci->db->where('id', $item->id)->update(TBL_BET_MANUAL_TOURNAMENT);
			}else{
				$correctAnswer = intval( $CAT->getCorrectAnswer() );
				if( intval($answer_array[0]) <= intval($correctAnswer) AND intval($correctAnswer) <= intval($answer_array[1]) ){
					$ci->db->set('result', 'won');
				}else{
					$ci->db->set('result', 'lost');
				}
				$ci->db->where('id', $item->id)->update(TBL_BET_MANUAL_TOURNAMENT);
			}
		}
	}
	
	//end class
}
